#!/bin/bash
sudo chmod +x ./src/mian.py
echo -e "
\033[34m      Copyright (C) 2024-2025 深圳市元创新科技, Inc. All Rights Reserved\033[0m

 
    \033[41;30m@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\033[0m
    \033[41;30m@                                                                    @\033[0m
    \033[41;30m@   ######    ######   #####   #     #     ####   #      #   #####   @\033[0m
    \033[41;30m@   #    ##   #          #     ##    #   ##   ##  #     #  ##    ##  @\033[0m
    \033[41;30m@   ## ###    ######     #     #  #  #   #     #   #    #  #      #  @\033[0m
    \033[41;30m@   #   ##    #          #     #   # #   #     #   #   #   #      #  @\033[0m
    \033[41;30m@   #     #   #          #     #    ##   ##   ##    ###     ##   ##  @\033[0m
    \033[41;30m@   #      #  ######   #####   #     #    ####       #       ####    @\033[0m
    \033[41;30m@                                                                    @\033[0m
    \033[41;30m@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\033[0m

            \033[34m软件名称：ReinovoWeb-元创兴科技教育机器web端控制软件\033[0m
                        \033[34m软件版本：v1.0.0 bate\033[0m
    
        *************欢迎使用ReinovoWeb软件安装程序**************
        *******此配置程序基于元创兴科技教育机器人ROS接口开发********
        ***************暂不支持配置相关ROS接口******************
        ****************************************************
    "     

echo -e "设置部署的设备型号\n请输入bobac3、oryxbot,\n如果是仿真环境请在设备型号后面添加sim。"
echo "************************************"
read -p "请输入:" robot_name

case $robot_name in
    bobac3)
        env_value="bobac3"
        # 定义要查询的包列表
        packages=("ar_pose" "auto_charging" "bobac3_description"  "bobac3_slam"  "cspc_lidar"  "map_manager" "rei_lidar_fuse"  "robot_audio"
"ar_track_alvar"  "berxel_camera"  "bobac3_navigation" "face_rec"  "pid_lib" "rei_robot_base"  "relative_move"  "robot_joy")
        ;;
    oryxbot)
        env_value="oryxbot"

        packages=("ar_pose" "auto_charging" "oryxbot_description"  "oryxbot_slam" "map_manager" "rei_lidar_fuse" "rei_ydlidar_nodelet"
"ar_track_alvar"  "oryxbot_navigation" "rei_robot_base"  "relative_move"  "robot_joy")
        ;;
    bobac3sim)
        env_value="bobac3sim"
        packages=("bobac3_description"  "bobac3_slam" "map_manager" "bobac3_navigation")
        ;;
    oryxbotsim)
        env_value="oryxbotsim"
        packages=("oryxbot_description"  "oryxbot_slam" "map_manager" "oryxbot_navigation")
        ;;
    *)
        echo -e "无效的选项，请输入bobac3、oryxbot,\n如果是仿真环境请在设备型号后面添加sim。"
        echo "************************************"
        exit 1
        ;;
esac

# 循环遍历包列表
for package in "${packages[@]}"
do
    if rospack find "$package" &> /dev/null; then
        echo "*************依赖查询中*************"
    else
        echo -e "\033[31mrospack报错： $package 不存在，可以访问我们的代码仓库下载安装\033[0m\n\033[33m**********[https://gitee.com/reinovo]************\033[0m"
                exit 1
    fi
done

# 定义要添加的环境变量
new_variable="export REI_ROBOT=$env_value"

# 检查环境变量是否已经存在于 .bashrc 文件中
if ! grep -q "export REI_ROBOT" ~/.bashrc; then
    # 若不存在，则添加到 .bashrc 文件中
    echo "$new_variable" >> ~/.bashrc
    echo "环境变量 $new_variable 已添加到 ~/.bashrc 文件中。"
else
    # 若已存在，则更新该环境变量
    sed -i "s/^export REI_ROBOT=.*/$new_variable/" ~/.bashrc
    echo "环境变量 $new_variable 已更新到 ~/.bashrc 文件中。"
fi
echo "************************************"

read -p "是否使用默认的地图文件路径(yes/no)：" updata_map
updata_map=$(echo "$updata_map" | tr '[:upper:]' '[:lower:]')
echo "************************************"
if [ "$updata_map" == "yes" ] || [ "$updata_map" == "y" ]; then
    if [[ $robot_name =~ sim ]]; then
        map_dir="/home/bobac3/bobac3_ws/src/bobac3_navigation/maps"
        face_data="/home/bobac3/bobac3_ws/src/face_data"
    else
        map_dir="~/.reinovo/maps"
        face_data="~/.reinovo/face_data"
    fi

    if ! [ -d "$map_dir" ]; then
        echo "创建地图目录"
        mkdir -p "$map_dir"
    fi
fi
if [ "$updata_map" == "no" ] || [ "$updata_map" == "n" ]; then
    read -p "请输入地图文件目录：" map_dir
    if ! [ -d "$map_dir" ]; then
        echo -e "\033[31m 未查询到目录，任务终止。 \033[0m"
        exit 1
    fi
fi
if [[ $robot_name =~ bobac ]]; then
    read -p "是否使用默认的人脸目录(yes/no)：" updata_face
    updata_face=$(echo "$updata_face" | tr '[:upper:]' '[:lower:]')
    echo "************************************"
    if [ "$updata_face" == "yes" ] || [ "$updata_face" == "y" ]; then
        if [[ $robot_name =~ sim ]]; then
            face_data="/home/bobac3/bobac3_ws/src/face_data"
        else
            face_data="~/.reinovo/face_data"
        fi

        if ! [ -d "$face_data" ]; then
            echo "创建人脸数据目录"
            mkdir -p "$face_data"
        fi
    fi
    if [ "$updata_face" == "no" ] || [ "$updata_face" == "n" ]; then
        read -p "请输入地图文件目录：" face_data
        if ! [ -d "$map_dir" ]; then
            echo -e "\033[31m 未查询到目录，任务终止。 \033[0m"
            exit 1
        fi
    fi
fi

# 检查环境变量是否已经存在于 .bashrc 文件中
if ! grep -q "export MAP_DIRECTORY" ~/.bashrc; then
    # 若不存在，则添加到 .bashrc 文件中
    echo "export MAP_DIRECTORY=$map_dir" >> ~/.bashrc
    echo "环境变量 export MAP_DIRECTORY=$map_dir已添加到 ~/.bashrc 文件中。"
else
    # 若已存在，则更新该环境变量
    sed -i "s#^export MAP_DIRECTORY=.*#export MAP_DIRECTORY=$map_dir#" ~/.bashrc
    # sed -i "s|^export MAP_DIRECTORY=.*|export MAP_DIRECTORY=$map_dir" ~/.bashrc
    echo "地图环境配置成功"
fi
echo "************************************"
if [[ $robot_name =~ bobac ]]; then
    if ! grep -q "export FACE_DIRECTORY" ~/.bashrc; then
        # 若不存在，则添加到 .bashrc 文件中
        echo "export FACE_DIRECTORY=$face_data" >> ~/.bashrc
        echo "环境变量 export FACE_DIRECTORY=$face_data 已添加到 ~/.bashrc 文件中。"
    else
        # 若已存在，则更新该环境变量
        # sed -i "s|^export FACE_DIRECTORY==.*|export FACE_DIRECTORY=$face_data" ~/.bashrc
        sed -i "s#^export FACE_DIRECTORY=.*#export FACE_DIRECTORY=$face_data#" ~/.bashrc
        echo "人脸环境配置成功"
    fi
fi
# 使配置文件立即生效
source ~/.bashrc

# 安装python依赖
sources=(
    "https://pypi.org/simple/"  # Python 官方源
    "https://pypi.tuna.tsinghua.edu.cn/simple/"  # 清华大学镜像源
    "https://mirrors.aliyun.com/pypi/simple/"  # 阿里云镜像源
    # "https://pypi.hustunique.com/simple/"  # 华中科技大学镜像源
)

fastest_source=""
min_time=1000

for source in "${sources[@]}"; do
    echo "测试pip源: $source"
    start_time=$(date +%s%N)
    wget -q --spider "$source" 2>/dev/null
    end_time=$(date +%s%N)
    elapsed_time=$(( (end_time - start_time) / 1000000 ))
    if [ $? -eq 0 ]; then
        echo "与 $source 的连接成功. 耗时: $elapsed_time ms"
        if [ $elapsed_time -lt $min_time ]; then
            min_time=$elapsed_time
            fastest_source=$source
        fi
    else
        echo "连接到 $source 失败."
    fi
    echo "-------------------"
done

if [ -n "$fastest_source" ]; then
    echo "******************pip升级中******************"
    python3 -m pip install -i $fastest_source pip -U
    echo "******************安装python依赖中******************"
    python3 -m pip install -i $fastest_source -r src/requirements.txt
    if [ $? -ne 0 ]; then
        echo "python依赖安装失败或未正确安装。"
        exit 1
    else
        echo "python依赖安装成功。"
    fi
else
    echo "未能找到python源,依赖安装失败。"
    exit 1
fi
# 安装ros依赖
sudo apt install ros-noetic-tf2-web-republisher 
if [ $? -ne 0 ]; then
    echo "tf2-web-republisher安装失败或未正确安装。"
    exit 1
else
    echo "tf2-web-republisher安装成功。"
fi
sudo apt install ros-noetic-rosbridge-server
if [ $? -ne 0 ]; then
    echo "rosbridge-server安装失败或未正确安装。"
    exit 1
else
    echo "rosbridge-server安装成功。"
fi

reiweb_path="reiwebpath=$(pwd)/src"

# 检查环境变量是否已经存在于 .bashrc 文件中
if grep -q "reiwebpath=" ./startup.sh; then
    # 若不存在，则添加到 .bashrc 文件中
    sed -i "s|^reiwebpath=.*|$reiweb_path|" ./startup.sh

fi
echo -e "\033[33m配置结束,是否需要运行程序？(yes/no)\033[0m"
read -p "：" is_run
is_run=$(echo "$is_run" | tr '[:upper:]' '[:lower:]')
echo "************************************"
if [ "$is_run" == "yes" ] || [ "$is_run" == "y" ]; then
    echo -e "\033[31m如果无法启动建议刷新环境并重新启动终端\033[0m"
    ./startup.sh
fi